from django.shortcuts import render

# Create your views here.

from publicHealth.models import PublicHealthDirection, PublicHealthClassification, PublicHealthArticles
from publicHealth.ser import PublicHealthDirectionSerializer, PublicHealthClassificationSerializer, PublicHealthArticlesSerializer

from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter

# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('name', 'publish_date')

# 大众健康方向 ListAPIView
class PublicHealthDirectionListView(ListAPIView):
    serializer_class = PublicHealthDirectionSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return PublicHealthDirection.objects.all()

# 大众健康分类 ListAPIView
class PublicHealthClassificationListView(ListAPIView):
    serializer_class = PublicHealthClassificationSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return PublicHealthClassification.objects.all()

# 大众健康文章 ListAPIView
class PublicHealthArticlesListView(ListAPIView):
    serializer_class = PublicHealthArticlesSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['publish_date', 'title']

    def get_queryset(self):
        return PublicHealthArticles.objects.all()